home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / supscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  12.0 KB  |  452 lines

  1. /*
  2.  * Copyright (c) 1992 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie Mellon
  23.  * the rights to redistribute these changes.
  24.  */
  25. /*
  26.  * supscan -- SUP Scan File Builder
  27.  *
  28.  * Usage: supscan [ -v ] collection [ basedir ]
  29.  *      supscan [ -v ] -f dirfile
  30.  *      supscan [ -v ] -s
  31.  *    -f    "file" -- use dirfile instead of system coll.dir
  32.  *    -s    "system" -- perform scan for system supfile
  33.  *    -v    "verbose" -- print messages as you go
  34.  *    collection    -- name of the desired collection if not -s
  35.  *    basedir        -- name of the base directory, if not
  36.  *               the default or recorded in coll.dir
  37.  *    dirfile        -- name of replacement for system coll.dir.
  38.  *
  39.  **********************************************************************
  40.  * HISTORY
  41.  * $Log: supscan.c,v $
  42.  * Revision 1.1.1.1  1993/05/21  14:52:19  cgd
  43.  * initial import of CMU's SUP to NetBSD
  44.  *
  45.  * Revision 1.14  92/08/11  12:08:30  mrt
  46.  *     Picked up Brad's deliniting and variable argument changes
  47.  *     [92/08/10            mrt]
  48.  * 
  49.  * Revision 1.13  92/02/08  18:04:44  dlc
  50.  *     Once again revised localhost().  Do not use gethostbyname() at
  51.  *     all, but assume that the host names in the coll.host file are at
  52.  *     least a prefix of the fully qualified name.  Modcoll (and related
  53.  *     scripts) will maintain this fact.
  54.  *     [92/02/08            dlc]
  55.  * 
  56.  * Revision 1.12  91/08/17  23:35:31  dlc
  57.  *     Changes to localhost() function:
  58.  *         - Use host name in kernel for local host name; assume it is
  59.  *           fully qualified.
  60.  *         - If gethostbyname() of host to see if we are the repository
  61.  *           fails, with TRY_AGAIN or NO_RECOVERY, then use the "host"
  62.  *           parameter.  Print a diagnostic in this case.
  63.  *     [91/08/17            dlc]
  64.  * 
  65.  * Revision 1.11  90/04/04  10:53:01  dlc
  66.  *     Changed localhost to retry getting the local host name 4 times with
  67.  *     30 second sleep intervals before aborting; after 4 tries, things are
  68.  *     probably too messed up for the supscan to do anything useful
  69.  *     [90/04/04            dlc]
  70.  * 
  71.  * Revision 1.10  89/08/03  19:49:33  mja
  72.  *     Updated to use v*printf() in place of _doprnt().
  73.  *     [89/04/19            mja]
  74.  * 
  75.  * Revision 1.9  89/06/18  14:41:37  gm0w
  76.  *     Fixed up some notify messages of errors to use "SUP:" prefix.
  77.  *     [89/06/18            gm0w]
  78.  * 
  79.  * 13-May-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
  80.  *    Changed goaway to longjmp back to top-level to scan next
  81.  *    collection. [V7.6]
  82.  *
  83.  * 19-Feb-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
  84.  *    Added -f <filename> switch to scan all (or part) of the
  85.  *    collections in a file of collection/base-directory pairs.
  86.  *    [V7.5]
  87.  *
  88.  * 27-Dec-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
  89.  *    Removed nameserver support (which means to use a new
  90.  *    datafile).
  91.  *
  92.  * 09-Sep-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
  93.  *    Use case-insensitive hostname comparison.
  94.  *
  95.  * 28-Jun-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
  96.  *    Added code for "release" support. [V6.4]
  97.  *
  98.  * 05-Jan-86  Glenn Marcy (gm0w) at Carnegie-Mellon University
  99.  *    Changed collection setup errors to be non-fatal. [V5.3]
  100.  *
  101.  * 29-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
  102.  *    Moved most of the scanning code to scan.c. [V4.2]
  103.  *
  104.  * 02-Nov-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
  105.  *    Added "-s" option.
  106.  *
  107.  * 22-Sep-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
  108.  *    Merged 4.1 and 4.2 versions together.
  109.  *
  110.  * 04-Jun-85  Steven Shafer (sas) at Carnegie-Mellon University
  111.  *    Created for 4.2 BSD.
  112.  *
  113.  **********************************************************************
  114.  */
  115.  
  116. #include <libc.h>
  117. #include <c.h>
  118. #include <netdb.h>
  119. #include <setjmp.h>
  120. #if __STDC__
  121. #include <stdarg.h>
  122. #else
  123. #include <varargs.h>
  124. #endif
  125. #include <sys/time.h>
  126. #include <sys/types.h>
  127. #ifdef _ABI_SOURCE
  128. #include <sys/utsname.h>
  129. #endif
  130. #include "sup.h"
  131.  
  132. #ifdef    lint
  133. /*VARARGS1*//*ARGSUSED*/
  134. static void quit(status) {};
  135. #endif    /* lint */
  136.  
  137. #define PGMVERSION 6
  138.  
  139. /*******************************************
  140.  ***    D A T A   S T R U C T U R E S    ***
  141.  *******************************************/
  142.  
  143. struct collstruct {            /* one per collection to be upgraded */
  144.     char *Cname;            /* collection name */
  145.     char *Cbase;            /* local base directory */
  146.     char *Cprefix;            /* local collection pathname prefix */
  147.     struct collstruct *Cnext;    /* next collection */
  148. };
  149. typedef struct collstruct COLLECTION;
  150.  
  151. /*********************************************
  152.  ***    G L O B A L   V A R I A B L E S    ***
  153.  *********************************************/
  154.  
  155. int trace;                /* -v flag */
  156.  
  157. COLLECTION *firstC;            /* collection list pointer */
  158. char *collname;                /* collection name */
  159. char *basedir;                /* base directory name */
  160. char *prefix;                /* collection pathname prefix */
  161. long lasttime = 0;            /* time of last upgrade */
  162. long scantime;                /* time of this scan */
  163. int newonly = FALSE;            /* new files only */
  164. jmp_buf sjbuf;                /* jump location for errors */
  165.  
  166. TREELIST *listTL;    /* list of all files specified by <coll>.list */
  167. TREE *listT;        /* final list of files in collection */
  168. TREE *refuseT = NULL;    /* list of all files specified by <coll>.list */
  169.  
  170. long time ();
  171.  
  172. /*************************************
  173.  ***    M A I N   R O U T I N E    ***
  174.  *************************************/
  175.  
  176. main (argc,argv)
  177. int argc;
  178. char **argv;
  179. {
  180.     register COLLECTION *c;
  181.  
  182.     init (argc,argv);        /* process arguments */
  183.     for (c = firstC; c; c = c->Cnext) {
  184.         collname = c->Cname;
  185.         basedir = c->Cbase;
  186.         prefix = c->Cprefix;
  187.         (void) chdir (basedir);
  188.         scantime = time ((long *)NULL);
  189.         printf ("SUP Scan for %s starting at %s",collname,
  190.             ctime (&scantime));
  191.         (void) fflush (stdout);
  192.         if (!setjmp (sjbuf)) {
  193.             makescanlists (); /* record names in scan files */
  194.             scantime = time ((long *)NULL);
  195.             printf ("SUP Scan for %s completed at %s",collname,
  196.                 ctime (&scantime));
  197.         } else
  198.             printf ("SUP: Scan for %s aborted at %s",collname,
  199.                 ctime (&scantime));
  200.         (void) fflush (stdout);
  201.     }
  202.     while (c = firstC) {
  203.         firstC = firstC->Cnext;
  204.         free (c->Cname);
  205.         free (c->Cbase);
  206.         if (c->Cprefix)  free (c->Cprefix);
  207.         free ((char *)c);
  208.     }
  209.     exit (0);
  210. }
  211.  
  212. /*****************************************
  213.  ***    I N I T I A L I Z A T I O N    ***
  214.  *****************************************/
  215.  
  216. usage ()
  217. {
  218.     fprintf (stderr,"Usage: supscan [ -v ] collection [ basedir ]\n");
  219.     fprintf (stderr,"       supscan [ -v ] -f dirfile\n");
  220.     fprintf (stderr,"       supscan [ -v ] -s\n");
  221.     exit (1);
  222. }
  223.  
  224. init (argc,argv)
  225. int argc;
  226. char **argv;
  227. {
  228.     char buf[STRINGLENGTH],fbuf[STRINGLENGTH],*p,*q;
  229.     FILE *f;
  230.     COLLECTION **c, *getcoll();
  231.     int fflag,sflag;
  232.     char *filename;
  233.  
  234.     trace = FALSE;
  235.     fflag = FALSE;
  236.     sflag = FALSE;
  237.     while (argc > 1 && argv[1][0] == '-') {
  238.         switch (argv[1][1]) {
  239.         case 'f':
  240.             fflag = TRUE;
  241.             if (argc == 2)
  242.                 usage ();
  243.             --argc;
  244.             argv++;
  245.             filename = argv[1];
  246.             break;
  247.         case 'v':
  248.             trace = TRUE;
  249.             break;
  250.         case 's':
  251.             sflag = TRUE;
  252.             break;
  253.         default:
  254.             fprintf (stderr,"supscan: Invalid flag %s ignored\n",argv[1]);
  255.             (void) fflush (stderr);
  256.         }
  257.         --argc;
  258.         argv++;
  259.     }
  260.     if (!fflag) {
  261.         (void) sprintf (fbuf,FILEDIRS,DEFDIR);
  262.         filename = fbuf;
  263.     }
  264.     if (sflag) {
  265.         if (argc != 1)
  266.             usage ();
  267.         firstC = NULL;
  268.         c = &firstC;
  269.         (void) sprintf (buf,FILEHOSTS,DEFDIR);
  270.         if ((f = fopen (buf,"r")) == NULL)
  271.             quit (1,"supscan: Unable to open %s\n",buf);
  272.         while ((p = fgets (buf,STRINGLENGTH,f)) != NULL) {
  273.             q = index (p,'\n');
  274.             if (q)  *q = 0;
  275.             if (index ("#;:",*p))  continue;
  276.             collname = nxtarg (&p," \t=");
  277.             p = skipover (p," \t=");
  278.             if (!localhost (p))  continue;
  279.             *c = getcoll(filename,salloc (collname),
  280.                     (char *)NULL);
  281.             if (*c)  c = &((*c)->Cnext);
  282.         }
  283.         (void) fclose (f);
  284.         return;
  285.     }
  286.     if (argc < 2 && fflag) {
  287.         firstC = NULL;
  288.         c = &firstC;
  289.         if ((f = fopen (filename,"r")) == NULL)
  290.             quit (1,"supscan: Unable to open %s\n",filename);
  291.         while (p = fgets (buf,STRINGLENGTH,f)) {
  292.             q = index (p,'\n');
  293.             if (q)  *q = 0;
  294.             if (index ("#;:",*p))  continue;
  295.             q = nxtarg (&p," \t=");
  296.             p = skipover (p," \t=");
  297.             *c = getcoll(filename,salloc (q),salloc (p));
  298.             if (*c)  c = &((*c)->Cnext);
  299.         }
  300.         (void) fclose (f);
  301.         return;
  302.     }
  303.     if (argc < 2 || argc > 3)
  304.         usage ();
  305.     firstC = getcoll(filename,salloc (argv[1]),
  306.             argc > 2 ? salloc (argv[2]) : (char *)NULL);
  307. }
  308.  
  309. COLLECTION *
  310. getcoll(filename, collname, basedir)
  311. register char *filename,*collname,*basedir;
  312. {
  313.     char buf[STRINGLENGTH],*p,*q;
  314.     FILE *f;
  315.     COLLECTION *c;
  316.  
  317.     if (basedir == NULL) {
  318.         if (f = fopen (filename,"r")) {
  319.             while (p = fgets (buf,STRINGLENGTH,f)) {
  320.                 q = index (p,'\n');
  321.                 if (q)  *q = 0;
  322.                 if (index ("#;:",*p))  continue;
  323.                 q = nxtarg (&p," \t=");
  324.                 if (strcmp (q,collname) == 0) {
  325.                     p = skipover (p," \t=");
  326.                     basedir = salloc (p);
  327.                     break;
  328.                 }
  329.             }
  330.             (void) fclose (f);
  331.         }
  332.         if (basedir == NULL) {
  333.             (void) sprintf (buf,FILEBASEDEFAULT,collname);
  334.             basedir = salloc (buf);
  335.         }
  336.     }
  337.     if (chdir(basedir) < 0) {
  338.         fprintf (stderr,"supscan:  Can't chdir to base directory %s for %s\n",
  339.             basedir,collname);
  340.         return (NULL);
  341.     }
  342.     prefix = NULL;
  343.     (void) sprintf (buf,FILEPREFIX,collname);
  344.     if (f = fopen (buf,"r")) {
  345.         while (p = fgets (buf,STRINGLENGTH,f)) {
  346.             q = index (p,'\n');
  347.             if (q) *q = 0;
  348.             if (index ("#;:",*p))  continue;
  349.             prefix = salloc (p);
  350.             if (chdir(prefix) < 0) {
  351.                 fprintf (stderr,"supscan: can't chdir to %s from base directory %s for %s\n",
  352.                     prefix,basedir,collname);
  353.                 return (NULL);
  354.             }
  355.             break;
  356.         }
  357.         (void) fclose (f);
  358.     }
  359.     if ((c = (COLLECTION *) malloc (sizeof(COLLECTION))) == NULL)
  360.         quit (1,"supscan: can't malloc collection structure\n");
  361.     c->Cname = collname;
  362.     c->Cbase = basedir;
  363.     c->Cprefix = prefix;
  364.     c->Cnext = NULL;
  365.     return (c);
  366. }
  367. #if __STDC__
  368. goaway (char *fmt,...)
  369. #else
  370. /*VARARGS*//*ARGSUSED*/
  371. goaway (va_alist)
  372. va_dcl
  373. #endif
  374. {
  375. #if !__STDC__
  376.     char *fmt;
  377. #endif
  378.     va_list ap;
  379.  
  380. #if __STDC__
  381.     va_start(ap,fmt);
  382. #else
  383.     va_start(ap);
  384.     fmt = va_arg(ap,char *);
  385. #endif
  386.  
  387.     vfprintf(stderr, fmt, ap);
  388.     va_end(ap);
  389.     (void) putc ('\n',stderr);
  390.     (void) fflush (stderr);
  391.     longjmp (sjbuf,TRUE);
  392. }
  393.  
  394. int localhost (host)
  395. register char *host;
  396. {
  397. #ifdef _ABI_SOURCE
  398.     static struct utsname myhost;
  399. #else
  400.     static char myhost[STRINGLENGTH];
  401. #endif
  402.     static int myhostlen;
  403.     register int hostlen;
  404.  
  405. #ifdef _ABI_SOURCE
  406.     if (*(myhost.nodename) == '\0') {
  407.         /*
  408.          * We assume that the host name in the kernel is the
  409.          * fully qualified form.
  410.          */
  411.         if (uname (&myhost) < 0) {
  412.             quit (1,"supscan: can't get kernel host name\n");
  413.         }
  414.         myhostlen = strlen(myhost.nodename);
  415.     }
  416. #else
  417.     if (*myhost == '\0') {
  418.         /*
  419.          * We assume that the host name in the kernel is the
  420.          * fully qualified form.
  421.          */
  422.         if (gethostname (myhost,sizeof (myhost)) < 0) {
  423.             quit (1,"supscan: can't get kernel host name\n");
  424.         }
  425.         myhostlen = strlen(myhost);
  426.     }
  427. #endif
  428.  
  429.     /*
  430.      * Here, we assume that the 'host' parameter from the
  431.      * coll.host file is at least a prefix of the fully qualified
  432.      * host name of some machine.  This will be true when modcoll(8)
  433.      * (and related scripts) maintain the relevant files, but if
  434.      * a person makes a manual change, problems could result.  In
  435.      * particular, if a nicname, such as "Y" for "GANDALF.CS.CMU.EDU"
  436.      * is present in the coll.host file, things will not work as
  437.      * expected.
  438.      */
  439.  
  440.     hostlen = strlen(host);
  441.  
  442. #ifdef _ABI_SOURCE
  443.     return(strncasecmp (myhost.nodename,
  444.                 host,
  445.                 hostlen < myhostlen ? hostlen : myhostlen) == 0);
  446. #else
  447.     return(strncasecmp (myhost,
  448.                 host,
  449.                 hostlen < myhostlen ? hostlen : myhostlen) == 0);
  450. #endif
  451. }
  452.